/**
 * WebDownload.java
 * Version: 1.0
 * Created in 06/08/2013
 * Created by Gerson Groth
 * 
 * Copyright
 */
package webcrawler.activities.view.download;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import webcrawler.activities.data.Data;
import webcrawler.activities.data.DataFile;
import webcrawler.activities.data.DataManager;
import webcrawler.activities.webcrawler.WebCrawler;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

/**
 * The Class WebDownload.
 * 
 * @author Gerson Groth
 */
public class WebDownload extends JDialog {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/** The content panel. */
	private final JPanel contentPanel = new JPanel();

	/** The jtf xml. */
	private JTextField jtfXml;

	/** The jtf tcx. */
	private JTextField jtfTcx;

	/** The jtf gpx. */
	private JTextField jtfGpx;

	/** The tcx path. */
	private String tcxPath = "";

	/** The xml path. */
	private String xmlPath = "";

	/** The gpx path. */
	private String gpxPath = "";

	/** The tcx file. */
	private JFileChooser tcxFile;

	/** The xml file. */
	private JFileChooser xmlFile;

	/** The gpx file. */
	private JFileChooser gpxFile;

	/** The JCheckBox xml file. */
	private JCheckBox chckbxXmlFile;

	/** The JCheckBox tcx file. */
	private JCheckBox chckbxTcxFile;

	/** The JCheckBox gpx file. */
	private JCheckBox chckbxGpxFile;

	/**
	 * Create the dialog.
	 */
	public WebDownload() {
		setFileChoosers();

		setDefaultConfigurations();

		createPanelDestinationFolder();

		createButtonExplore();

	}

	/**
	 * Creates the button explore.
	 */
	private void createButtonExplore() {
		JButton btnExplore = new JButton("Explore");
		btnExplore.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				explore(e);
			}
		});
		btnExplore.setBounds(157, 162, 89, 23);
		contentPanel.add(btnExplore);

	}

	/**
	 * Explore.
	 *
	 * @param e the e
	 */
	protected void explore(ActionEvent e) {
		if (chckbxGpxFile.isSelected() || chckbxTcxFile.isSelected()
				|| chckbxXmlFile.isSelected()) {
			Data data = Data.getInstance().getClone();
			data.setXmlFolder(replaceAll(jtfXml.getText()));
			data.setTcxFolder(replaceAll(jtfTcx.getText()));
			data.setGpxFolder(replaceAll(jtfGpx.getText()));
			data.setDownloadXml(chckbxXmlFile.isSelected());
			data.setDownloadGpx(chckbxGpxFile.isSelected());
			data.setDownloadTcx(chckbxTcxFile.isSelected());

			if (!"".equals(DataFile.getInstance().getPassword())
					&& !"".equals(DataFile.getInstance().getUsername())) {

				data.setPassword(DataManager.decryptPass(DataFile.getInstance()
						.getPassword()));
				data.setUserName(DataFile.getInstance().getUsername());
				data.setLogin(true);
			} else
				data.setLogin(false);

			WebCrawler crawler = new WebCrawler(data);
			crawler.setWebSearch(true);
			Thread webCrawler = new Thread(crawler);
			webCrawler.start();

			setVisible(false);
			dispose();

		} else {
			JOptionPane.showMessageDialog(null,
					"Choose at least one file type to download");
		}

	}

	/**
	 * Creates the panel destination folder.
	 */
	private void createPanelDestinationFolder() {
		JPanel destinationFolderPanel = new JPanel();
		destinationFolderPanel.setLayout(null);
		destinationFolderPanel.setBorder(new TitledBorder(UIManager

		.getBorder("TitledBorder.border"), "Destination Folder",

		TitledBorder.LEADING, TitledBorder.TOP, null, null));
		destinationFolderPanel.setBounds(10, 11, 414, 140);
		contentPanel.add(destinationFolderPanel);

		JPanel panelXml = createPanelXml();
		destinationFolderPanel.add(panelXml);
		destinationFolderPanel.add(chckbxXmlFile);

		JPanel panelTcx = createPanelTcx();
		destinationFolderPanel.add(panelTcx);
		destinationFolderPanel.add(chckbxTcxFile);

		JPanel panelGpx = createPanelGpx();
		destinationFolderPanel.add(panelGpx);
		destinationFolderPanel.add(chckbxGpxFile);

	}

	/**
	 * Creates the panel gpx.
	 *
	 * @return the j panel
	 */
	private JPanel createPanelGpx() {
		chckbxGpxFile = new JCheckBox("Gpx File");

		chckbxGpxFile.setSelected(true);
		chckbxGpxFile.setBounds(6, 25, 77, 23);

		JPanel gpxPanel = new JPanel();
		gpxPanel.setLayout(null);
		gpxPanel.setBounds(89, 25, 315, 23);

		final JButton btnGpxSearch = new JButton("Search");
		btnGpxSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				searchButtonGpx(e);
			}
		});
		btnGpxSearch.setBounds(226, 0, 89, 23);
		gpxPanel.add(btnGpxSearch);

		jtfGpx = new JTextField();
		jtfGpx.setColumns(10);
		jtfGpx.setBounds(0, 1, 216, 20);
		gpxPanel.add(jtfGpx);

		chckbxGpxFile.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				if (chckbxGpxFile.isSelected()) {
					jtfGpx.setEnabled(true);
					btnGpxSearch.setEnabled(true);
				} else {
					jtfGpx.setText("");
					jtfGpx.setEnabled(false);
					btnGpxSearch.setEnabled(false);
				}
			}
		});

		return gpxPanel;
	}

	/**
	 * Creates the panel tcx.
	 *
	 * @return the j panel
	 */
	private JPanel createPanelTcx() {
		chckbxTcxFile = new JCheckBox("Tcx File");

		chckbxTcxFile.setSelected(true);
		chckbxTcxFile.setBounds(6, 61, 77, 23);

		JPanel tcxPanel = new JPanel();
		tcxPanel.setLayout(null);
		tcxPanel.setBounds(89, 61, 315, 23);

		final JButton btnTcxSearch = new JButton("Search");
		btnTcxSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				searchButtonTcx(e);
			}
		});
		btnTcxSearch.setBounds(226, 0, 89, 23);
		tcxPanel.add(btnTcxSearch);

		jtfTcx = new JTextField();
		jtfTcx.setColumns(10);
		jtfTcx.setBounds(0, 1, 216, 20);
		tcxPanel.add(jtfTcx);

		chckbxTcxFile.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				if (chckbxTcxFile.isSelected()) {
					jtfTcx.setEnabled(true);
					btnTcxSearch.setEnabled(true);
				} else {
					jtfTcx.setText("");
					jtfTcx.setEnabled(false);
					btnTcxSearch.setEnabled(false);
				}
			}
		});

		return tcxPanel;
	}

	/**
	 * Creates the panel xml.
	 *
	 * @return the j panel
	 */
	private JPanel createPanelXml() {
		chckbxXmlFile = new JCheckBox("Xml File");

		chckbxXmlFile.setSelected(true);
		chckbxXmlFile.setBounds(6, 97, 77, 23);

		JPanel xmlPanel = new JPanel();
		xmlPanel.setLayout(null);
		xmlPanel.setBounds(89, 97, 315, 23);

		final JButton btnXmlSearch = new JButton("Search");
		btnXmlSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				searchButtonXml(e);
			}
		});
		btnXmlSearch.setBounds(226, 0, 89, 23);
		xmlPanel.add(btnXmlSearch);

		jtfXml = new JTextField();
		jtfXml.setColumns(10);
		jtfXml.setBounds(0, 1, 216, 20);
		xmlPanel.add(jtfXml);

		chckbxXmlFile.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				if (chckbxXmlFile.isSelected()) {
					jtfXml.setEnabled(true);
					btnXmlSearch.setEnabled(true);
				} else {
					jtfXml.setText("");
					jtfXml.setEnabled(false);
					btnXmlSearch.setEnabled(false);
				}
			}
		});
		return xmlPanel;
	}

	/**
	 * Sets the default configurations.
	 */
	private void setDefaultConfigurations() {
		setTitle("Web Activities Search");
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 242);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
	}

	/**
	 * Sets the file choosers.
	 */
	private void setFileChoosers() {
		tcxFile = new JFileChooser();
		xmlFile = new JFileChooser();
		gpxFile = new JFileChooser();
		tcxFile.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		xmlFile.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		gpxFile.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

	}

	/**
	 * Search button gpx.
	 * 
	 * @param e
	 *            the e
	 */
	protected void searchButtonGpx(ActionEvent e) {
		int result = gpxFile.showOpenDialog(this);
		if (result == JFileChooser.APPROVE_OPTION) {
			gpxPath = gpxFile.getSelectedFile().getAbsolutePath();
			while (gpxPath.indexOf('\\') != -1) {
				gpxPath = gpxPath.replace('\\', '/');
			}
			this.jtfGpx.setText(gpxPath);
		}

	}

	/**
	 * Search button tcx.
	 * 
	 * @param e
	 *            the e
	 */
	protected void searchButtonTcx(ActionEvent e) {
		int result = tcxFile.showOpenDialog(this);
		if (result == JFileChooser.APPROVE_OPTION) {
			tcxPath = tcxFile.getSelectedFile().getAbsolutePath();
			while (tcxPath.indexOf('\\') != -1) {
				tcxPath = tcxPath.replace('\\', '/');
			}
			this.jtfTcx.setText(tcxPath);
		}

	}

	/**
	 * Search button xml.
	 * 
	 * @param e
	 *            the e
	 */
	private void searchButtonXml(ActionEvent e) {
		int result = xmlFile.showOpenDialog(this);
		if (result == JFileChooser.APPROVE_OPTION) {
			xmlPath = xmlFile.getSelectedFile().getAbsolutePath();
			while (xmlPath.indexOf('\\') != -1) {
				xmlPath = xmlPath.replace('\\', '/');
			}
			this.jtfXml.setText(xmlPath);
		}

	}

	/**
	 * Replace all.
	 * 
	 * @param s
	 *            the s
	 * @return the string
	 */
	private String replaceAll(String s) {
		while (s.indexOf('\\') != -1) {
			s = s.replace('\\', '/');
		}
		return s;
	}

}
